package twentytwenty;

import java.util.*;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-10 9:21
 * @Modified By:
 */
public class ContainsDuplicate_217 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 5, 1};

    }

    // 暴力匹配，两个for循环 时间超限
    public static boolean containsDuplicate1(int[] nums) {
        int len = nums.length;
        Arrays.sort(nums);
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (nums[i] == nums[j]) {
                    return true;
                }
            }
        }
        return false;
    }

    // 排序暴力匹配
    public boolean containsDuplicate2(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 1; ++i) {
            if (nums[i] == nums[i + 1]) return true;
        }
        return false;
    }

    public boolean containsDuplicate3(int[] nums) {
        List<Integer> set = new ArrayList<>(nums.length);
        for (int x : nums) {
            if (set.contains(x)) return true;
            set.add(x);
        }
        return false;
    }
}
